package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.model.Group;
import com.cn.jdia.tas.model.Groupmember;
import com.cn.jdia.tas.model.T_attrecord;
import com.cn.jdia.tas.model.T_employee;
import com.cn.jdia.tas.service.IAttrecordService;
import com.cn.jdia.tas.service.IEmployeeService;
import com.cn.jdia.tas.service.IGroupmemberService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class GroupController {

	@Resource
	private IGroupmemberService groupMemberService = null;

	@Resource
	private IAttrecordService recordService = null;

	@Resource
	private IEmployeeService employeeService = null;

	@RequestMapping("/belonggroupquery")
	public String getBelongedGroup(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();

		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String pid = pmap.get("pid");
		System.out.println(pid);

		List<Groupmember> belongedGroup = groupMemberService.getBelongedGroup(pid);

		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map;

		for (Groupmember groupmember : belongedGroup) {
			map = new HashMap<>();

			map.put("gid", groupmember.getGroupid());
			map.put("gname", groupmember.getGroup().getName());
			map.put("gnum", groupmember.getGroup().getNumber());
			list.add(map);
		}

		out.write(gson.toJson(list));
		out.flush();
		out.close();
		return null;
	}

	@RequestMapping("/groupmemberquery")
	public String getGroupMember(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String gid = pmap.get("gid");

		List<Groupmember> memberList = groupMemberService.getGroupMember(Integer.parseInt(gid));
		List<String> employeeidList = new ArrayList<>();
		for (Groupmember member : memberList) {
			employeeidList.add(member.getEmployeeid());
		}

		DateTime dt = new DateTime();
		String date = dt.toString("yyyy-MM-dd");

		List<T_attrecord> groupRecord = recordService.getGroupRecord(date, employeeidList);
		List<Map<String, String>> retlist = new ArrayList<>();
		Map<String, String> tmap;
		List<String> recordList = new ArrayList<>();

		String tmpId = "";

		for (T_attrecord rec : groupRecord) {
			// System.out.println(rec.getEmployeeid() +
			// rec.getEmployee().getEmployeename() + rec.getCheckdate() + " "
			// + rec.getChecktime());

			// 已存在最早记录，跳过
			if (tmpId.equals(rec.getEmployeeid())) {
				continue;
			} else {
				tmap = new HashMap<>();
				tmap.put("id", rec.getEmployeeid());
				tmap.put("name", rec.getEmployee().getEmployeename());
				tmap.put("date", date);
				tmap.put("time", rec.getChecktime());
				tmap.put("loc", rec.getAttmachinename());
				tmap.put("check", "true");
				retlist.add(tmap);

				tmpId = rec.getEmployeeid();
				recordList.add(rec.getEmployeeid());
			}
		}

		List<String> diff = new ArrayList<String>();
		for (String str : employeeidList) {
			if (!recordList.contains(str)) {
				diff.add(str);
			}
		}

//		System.out.println(employeeidList);
//		System.out.println(recordList);
//		System.out.println(diff);

		if (diff.size() != 0) {
			List<T_employee> notCheckList = employeeService.getInterestList(diff);
			for (T_employee employee : notCheckList) {
				tmap = new HashMap<>();

				tmap.put("id", employee.getEmployeeid());
				tmap.put("name", employee.getEmployeename());
				tmap.put("check", "false");
				retlist.add(tmap);
			}
		}

		out.write(gson.toJson(retlist));
		out.flush();
		out.close();
		return null;
	}
}
